package com.zbcz.crm

import android.os.Bundle
import androidx.activity.viewModels
import androidx.lifecycle.MutableLiveData
import com.zbcz.crm.adapter.CommonListAdapter
import com.zbcz.crm.bean.CommonListBean
import com.zbcz.crm.databinding.ActivityUserCreateBinding
import com.zbcz.crm.util.IS_TEST_MODE
import com.zbcz.crm.util.onSingleClick
import com.zbcz.crm.viewmodel.UserAddViewModel

class CustomerAddActivity : BaseActivity() {

    private lateinit var mBinding: ActivityUserCreateBinding

    private val mUserAddViewModel: UserAddViewModel by viewModels()

    companion object {
        const val CUSTOMER_NAME = 1
        const val CUSTOMER_PHONE = 2
        const val CUSTOMER_MOBILE = 3
        const val CUSTOMER_SOURCE = 4
        const val CUSTOMER_INDUSTRY = 5
        const val CUSTOMER_LEVEL = 6
        const val DEAL_STATUS = 7
        const val CUSTOMER_WEBSITE = 8
        const val CUSTOMER_ADDRESS = 9
        const val REMARKS = 10
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mBinding = ActivityUserCreateBinding.inflate(layoutInflater)
        setContentView(mBinding.root)
        initList()
    }

    private var mAdapter: CommonListAdapter? = null

    private fun initList() {
        val mDataList = generateStartCommonList()
        mAdapter = CommonListAdapter(mDataList)
        mBinding.rcList.adapter = mAdapter
        mBinding.tvConfirm.setOnClickListener {
            mAdapter?.createCustomRequest()
                ?.let { customer ->
                    mUserAddViewModel.addCustomer(customer)
                }
        }
        mBinding.tvCancel.setOnClickListener {
            finish()
        }
        mUserAddViewModel.mResponse.observe(this) { isSuccess ->
            if (isSuccess) {
                finish()
            }
        }
        mBinding.ivBack.onSingleClick {
            finish()
        }
    }

    private fun generateStartCommonList(): List<CommonListBean> {
        return if (IS_TEST_MODE) listOf(
            return listOf(
                CommonListBean("客户名称", "请输入客户名称", CUSTOMER_NAME, "测试客户1"),
                CommonListBean("客户手机", "请输入客户手机", CUSTOMER_MOBILE, "18734831522"),
                CommonListBean("客户电话", "请输入客户电话", CUSTOMER_PHONE, "18734831522"),
                CommonListBean("客户来源", "请选择", CUSTOMER_SOURCE, ""),
                CommonListBean("客户行业", "请选择", CUSTOMER_INDUSTRY, ""),
                CommonListBean("客户级别", "请选择", CUSTOMER_LEVEL, ""),
                CommonListBean("成交状态", "请选择", DEAL_STATUS, ""),
                CommonListBean(
                    "客户网址",
                    "请输入客户网址",
                    CUSTOMER_WEBSITE,
                    "http://example.com"
                ),
                CommonListBean(
                    "详细地址",
                    "请输入客户详细地址",
                    CUSTOMER_ADDRESS,
                    "某个地方的详细地址"
                ),
                CommonListBean("备注", "请输入备注", REMARKS, "这是一个测试备注")
            )
        ) else listOf(
            CommonListBean("客户名称", "请输入客户名称", CUSTOMER_NAME, ""),
            CommonListBean("客户手机", "请输入客户手机", CUSTOMER_MOBILE, ""),
            CommonListBean("客户座机", "请输入客户电话", CUSTOMER_PHONE, ""),
            CommonListBean("客户来源", "请选择", CUSTOMER_SOURCE, ""),
            CommonListBean("客户行业", "请选择", CUSTOMER_INDUSTRY, ""),
            CommonListBean("客户级别", "请选择", CUSTOMER_LEVEL, ""),
            CommonListBean("成交状态", "请选择", DEAL_STATUS, ""),
            CommonListBean("客户网址", "请输入客户网址", CUSTOMER_WEBSITE, ""),
            CommonListBean(
                "详细地址",
                "请输入客户详细地址",
                CUSTOMER_ADDRESS,
                ""
            ),
            CommonListBean("备注", "请输入备注", REMARKS, "")
        )
    }

}